<?php 
namespace app\admin\controller;

use app\common\model\CompanyDepartment;
use app\common\model\CompanyJobpost;
use think\Exception;
use app\common\model\CompanyStaffDepart;
use app\common\model\CompanyStaff;
use app\common\model\CompanyStaffCommunication;

class Department extends Base
{
	/**
	 * 获取部门和岗位信息
	 */
	public function getJobInfo()
	{
		$depart = CompanyDepartment::where(['state' => 1])->select()->toArray();
		$post = CompanyJobpost::where(['state' => 1])->select()->toArray();
		return $this->success([
				'depart' => $depart,
				'post' => $post
		]);
	}
	/**
	 * 获取部门列表
	 */
	public function index()
	{
		$list = CompanyDepartment::where(['state' => 1])->select()->toArray();
		return $this->success($list);
	}
	
	/**
	 * 添加部门
	 * @throws Exception
	 */
	public function addDepart()
	{
		//获取客户端传递的数据
		$data = $this->request->param('');
		try {
			if(!$data['depart_name'])throw new Exception('请输入部门名称');
			if(!$data['depart_no'])throw new Exception('请输入部门编号');
			//重组数据
			$data['create_time'] = time();
			$data['update_time'] = time();
			CompanyDepartment::create($data);
		} catch (Exception $e) {
			return $this->error($e->getMessage());
		}
		return $this->success('部门添加成功');
	}
	
	/**
	 * 添加岗位
	 * @throws Exception
	 */
	public function addJobpost()
	{
		//获取客户端传递的数据
		$data = $this->request->param('');
		try {
			if(!$data['post_name'])throw new Exception('请输入岗位名称');
			if(!$data['post_no'])throw new Exception('请输入岗位编号');
			//重组数据
			$data['create_time'] = time();
			$data['update_time'] = time();
			CompanyJobpost::create($data);
		} catch (Exception $e) {
			return $this->error($e->getMessage());
		}
		return $this->success('岗位添加成功');
	}
	/**
	 * 获取岗位列表
	 */
	public function jobpost()
	{
		$list = CompanyJobpost::where(['state' => 1])->select()->toArray();
		return $this->success($list);
	}
	
	/**
	 * 根据部门查找员工列表
	 */
	public function getDepartStaff()
	{
		$id = $this->request->param('depart_id');
		$staff_ids = CompanyStaffDepart::where(['depart_id' => $id])->column('staff_id');
		$staff_list = CompanyStaff::where(['id' => ['in',$staff_ids]])->order('leave_state desc,id desc')->field('id,name,leave_state')->paginate()->toArray();
		foreach ($staff_list['data'] as &$rows) {
			$rows['communication'] = CompanyStaffCommunication::where(['staff_id' => $rows['id']])->find()->toArray();
		}
		return $this->success($staff_list);
	}
	
	/**
	 * 根据岗位查找员工列表
	 */
	public function getPostStaff()
	{
		$id = $this->request->param('depart_id');
		$staff_ids = CompanyStaffDepart::where(['post_id' => $id])->column('staff_id');
		$staff_list = CompanyStaff::where(['id' => ['in',$staff_ids]])->order('leave_state desc,id desc')->field('id,name,leave_state')->paginate()->toArray();
		foreach ($staff_list['data'] as &$rows) {
			$rows['communication'] = CompanyStaffCommunication::where(['staff_id' => $rows['id']])->find()->toArray();
		}
		return $this->success($staff_list);
	}
}